﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using MusicAlarm7.Model;
using System.Collections;
using System.Collections.Generic;
using MusicAlarm7.Resources;

namespace MusicAlarm7
{
    public partial class AddNotification : PhoneApplicationPage
    {
        AlarmItem alarm;
        bool isNew;

        public AddNotification() : this(App.ViewModel.CurrentAlarm) { }

        public AddNotification(AlarmItem alarm)
        {
            Debug.WriteLine(System.Globalization.CultureInfo.CurrentCulture.Name);

            InitializeComponent();
            BuildApplicationBar();
            if (alarm == null)
            {
                this.isNew = true;
                this.DataContext = this.alarm = new AlarmItem();
                this.alarm.AlarmTime = DateTime.Now;
                this.alarm.MusicIndex = 0;
                this.PageTitleBlock.Text = AppResources.EditPage_Title_New;
                this.alarm.RecurringDays = "0000000";
            }
            else
            {
                this.isNew = false;
                this.DataContext = this.alarm = alarm;
                this.PageTitleBlock.Text = AppResources.EditPage_Title_Edit;
            }
        }

        private void BuildApplicationBar()
        {
            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.IsMenuEnabled = true;
            this.ApplicationBar.IsVisible = true;
            this.ApplicationBar.Opacity = 1.0;
            this.ApplicationBar.Mode = ApplicationBarMode.Default;

            ApplicationBarIconButton savbtn = new ApplicationBarIconButton();
            savbtn.IconUri = new Uri("/Images/appbar.save.rest.png", UriKind.Relative);
            savbtn.Text = AppResources.EditPage_SavBtn;
            this.ApplicationBar.Buttons.Add(savbtn);
            savbtn.Click += savbtn_Click;

            ApplicationBarIconButton delbtn = new ApplicationBarIconButton();
            delbtn.IconUri = new Uri("/Images/appbar.delete.rest.png", UriKind.RelativeOrAbsolute);
            delbtn.Text = AppResources.EditPage_DelBtn;
            this.ApplicationBar.Buttons.Add(delbtn);
            delbtn.Click += new EventHandler(delbtn_Click);
            
        }

        void delbtn_Click(object sender, EventArgs e)
        {
            App.ViewModel.DeleteAlarm(this.alarm);
            App.ViewModel.Commit();

            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }

        void savbtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.alarm.IsEnabled = true;
                if (this.isNew)
                    App.ViewModel.AddAlarm(this.alarm);
                else
                    App.ViewModel.ModifyAlarm(this.alarm);
            }
            catch (Exception ex)
            {
                MessageBox.Show(AppResources.EditPage_Save_Alarm_Fail + ex.Message);
                App.ViewModel.Rollback();
            }
            App.ViewModel.Commit();

            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
    }
}